package com.ww.core.util.http;

import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.params.ConnPerRoute;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.scheme.SocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

public class MyHttpClient {
	private static DefaultHttpClient hc = null;;

	public static DefaultHttpClient getHttpClient() {
		if (null == hc) {
			hc = getClient();
		}
		return hc;
	}

	private static DefaultHttpClient getClient() {
		// Logger.info("private static DefaultHttpClient getClient()");
		HttpParams params = new BasicHttpParams();
		ConnPerRoute connPerRoute = new ConnPerRouteBean(100);
		ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
		ConnManagerParams.setMaxTotalConnections(params, 100);
		// Turn off stale checking. Our connections break all the time anyway,
		// and it's not worth it to pay the penalty of checking every time.
		HttpConnectionParams.setStaleCheckingEnabled(params, false);
		// Sets up the http part of the service.
		final SchemeRegistry supportedSchemes = new SchemeRegistry();
		// Default connection timeout of 3 seconds
		// Default socket timeout of 100 seconds. Tweak to taste.
		HttpConnectionParams.setConnectionTimeout(params, 8 * 1000);
		HttpConnectionParams.setSoTimeout(params, 100 * 1000);
		HttpConnectionParams.setSocketBufferSize(params, 8192);
		// Register the "http" protocol scheme, it is required
		// by the default operator to look up socket factories.
		final SocketFactory sf = PlainSocketFactory.getSocketFactory();
		supportedSchemes.register(new Scheme("http", sf, 80));

		final ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, supportedSchemes);
		DefaultHttpClient httpClient = new DefaultHttpClient(ccm, params);
		httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);

		// if (RemoteResourceFetcher.isCmwap) {
		// HttpHost proxy = new HttpHost("10.0.0.172", 80);
		// httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
		// proxy);
		// }
		// if (RemoteResourceFetcher.isUniwap) {
		// HttpHost proxy = new HttpHost("10.0.0.172", 80);
		// httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
		// proxy);
		// }
		// if (RemoteResourceFetcher.isCtwap) {
		// HttpHost proxy = new HttpHost("10.0.0.200", 80);
		// httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
		// proxy);
		// }

		return httpClient;
	}

}
